package cn.com.yiqi.wsc.service.tag.impl;

import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultStatusEnum;
import cn.com.yiqi.wsc.dao.tags.TagDao;
import cn.com.yiqi.wsc.dao.tags.criteria.TagCriteria;
import cn.com.yiqi.wsc.dao.tags.entity.TagExample;
import cn.com.yiqi.wsc.service.tag.TagService;
import cn.com.yiqi.wsc.service.user.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 杨贵森
 * @ClassName:TagServiceImpl
 * @Title: TagServiceImpl
 * @Description: TODO
 * @date 2017/3/23
 */
@Service
public class TagServiceImpl implements TagService {

    @Autowired
    TagDao tagDao;
    @Autowired
    UserService userService;

    @Override
    public Result getTagList(TagCriteria tagCriteria) {
        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());
        if(tagCriteria.getIsAll()==0){
            PageHelper.startPage(tagCriteria.getCurrentPage(),tagCriteria.getPageSize());
        }

        PageInfo<TagExample> tagExampleList = new PageInfo<>(tagDao.selectTagList(tagCriteria));
        result.putData("total",tagExampleList.getTotal());
        result.putData("tagList",tagExampleList.getList());
        return result;
    }

    @Override
    @Transactional
    public Result addTag(TagExample tagExample) {
        //tagExample.setInsertUserId(userService.getCurrentUserId());
        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());
        TagExample tagExample1 = (TagExample) selectOneTagByName(tagExample).getData().get("Tag");
        if(tagExample1!=null){
            result.putData("eixsted",1);
            return result;
        }
        result.putData("addTag",tagDao.insert(tagExample));
        return result;
    }

    @Override
    @Transactional
    public Result deleteTag(TagExample tagExample) {
        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());
        result.putData("deleteTag",tagDao.delete(tagExample.getId()));
        return result;
    }

    @Override
    @Transactional
    public Result editTag(TagExample tagExample) {
        tagExample.setUpdateUserId(userService.getCurrentUserId());
        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());
        result.putData("editTag",tagDao.update(tagExample));
        return result;
    }

    @Override
    public Result selectOneTagById(TagExample tagExample) {
        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());
        result.putData("selectTag",tagDao.selectTagById(tagExample));
        return result;
    }

    @Override
    public Result selectOneTagByName(TagExample tagExample) {
        Result result = new Result();
        result.setStatus(ResultStatusEnum.操作成功.getCode());
        result.putData("Tag",tagDao.selectTagByTagName(tagExample));
        return result;
    }
}
